# event
from Cube.fsm.EventDefine import event_MAINMENU
from EventDefine import event_MAPMGRFSM_INTERMEZZO, event_MAPMGRFSM_THEME, \
                        event_MAPMGRFSM_FINALE, event_MAPMGRFSM_BACKTOMAINMENU
from Cube.map import MapMgrGet, MapBodyControllerGet
# fsm
from direct.fsm import FSM

class QMapMgrFSM(FSM.FSM):
    def __init__(self):
        FSM.FSM.__init__(self, name = 'Map Mgr FSM')
        self.MapMgr = MapMgrGet()
        self.MapBodyController = MapBodyControllerGet()
        if self.MapMgr.CurrentMapHeader is None:
            self.demand('Prelude')
        else:
            # MapHeader has alreay prepared
            self.demand('Theme')

    def enterPrelude(self):
        # decide MapHeader
        self.acceptOnce(event_MAPMGRFSM_THEME, self.demand, ["Theme"])
        self.PreludeFrame = self.MapMgr.PreludeFrameGet()

    def exitPrelude(self):
        self.ignoreAll()
        self.PreludeFrame.destroy()

    def enterTheme(self):
        # MapBodyController ( load MapBody )
        self.acceptOnce(event_MAPMGRFSM_INTERMEZZO, self.demand, ["Intermezzo"])
        self.MapBodyController.Initial()

    def exitTheme(self):
        self.ignoreAll()
        self.MapBodyController.destroy()

    def enterIntermezzo(self):
        # prepare next MapHeader
        self.acceptOnce(event_MAPMGRFSM_THEME, self.demand, ["Theme"])
        self.acceptOnce(event_MAPMGRFSM_FINALE, self.demand, ["Finale"])
        self.IntermezzoFrame = self.MapMgr.IntermezzoFrameGet()
        if self.IntermezzoFrame is None:
            self.demand('Finale')

    def exitIntermezzo(self):
        self.ignoreAll()
        if self.IntermezzoFrame is not None:
            self.FinaleFrame.destroy()

    def enterFinale(self):
        self.acceptOnce(event_MAPMGRFSM_BACKTOMAINMENU, self.demand, ["MainMenu"])
        self.FinaleFrame = self.MapMgr.FinaleFrameGet()
        if self.FinaleFrame is None:
            self.demand('MainMenu')

    def exitFinale(self):
        self.ignoreAll()
        if self.FinaleFrame is not None:
            self.FinaleFrame.destroy()

    def enterMainMenu(self):
        self.MapMgr.destroy()
        messenger.send(event_MAINMENU)

